package com.hmdp.exceptionHandler;

import com.hmdp.entity.dto.Result;
import com.hmdp.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@ResponseBody
@ControllerAdvice(annotations = {Controller.class, RestController.class})
public class GlobalExceptionHandler {
    @ExceptionHandler(MyException.class)
    public Result myExceptionHandler(MyException exception)
    {
        log.error(exception.getMessage());
        return Result.fail(exception.getMessage());
    }
    @ExceptionHandler(RuntimeException.class)
    public Result               handleRuntimeException(RuntimeException e) {
        log.error(e.toString(), e);
        return Result.fail("服务器异常");
    }
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception exception)
    {
        log.error(exception.getMessage());
        return Result.fail(exception.getMessage());
    }
}
